package com.douqu.game.core.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.bean.core.pay.alipay.AlipayConfig;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author wangzhenfei
 *         2018-03-26 14:15
 */
public class AliPayUtils {
    /**
     *  创建支付宝订单
     * @return
     */
    public static  String createOrder(String money, String rechargeParam){
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                AlipayConfig.appid, AlipayConfig.private_key,
                "json", AlipayConfig.charset, AlipayConfig.ali_public_key, AlipayConfig.SIGN_RSA2);
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody("我是测试数据");
        model.setSubject("道具购买");
        //支付宝 支付交易号
        model.setOutTradeNo(generatePayBatchNo());
        model.setTimeoutExpress("30m");
        model.setTotalAmount(money);
        model.setProductCode("QUICK_MSECURITY_PAY");
        model.setPassbackParams(rechargeParam);
        request.setBizModel(model);
        request.setNotifyUrl(AlipayConfig.notify_url);
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            String orderString = response.getBody();
            System.out.println(orderString);//就是orderString 可以直接给客户端请求，无需再做处理。
            return orderString;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * 生成支付交易号 eg:20150914100250-9545
     *
     * @return
     */
    public static synchronized String generatePayBatchNo()
    {
        String batchNo = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String datetime = sdf.format(new Date());
        int hashCode = UUID.randomUUID().toString().hashCode();
        if (hashCode < 0) {
            hashCode = hashCode * -1;
        }
        String hashCodeStr = String.valueOf(hashCode);
        if (hashCodeStr.length() > 16) {
            hashCodeStr = hashCodeStr.substring(0, 16);
        }
        batchNo = datetime + "-" + hashCodeStr;
        // int randomNo = new Random().nextInt(10000);
        // batchNo = datetime + "-" + randomNo;
        return batchNo;
    }


    public static void main(String[] args) {

        String APP_ID="2018032302432568";
        String APP_PRIVATE_KEY="MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDRCvUnFISs8YT0cy+HX2KXBx2Yd9l2YvEOe1Tsnsni/IFMqgNF5lnzLmqPnrh5M+4a42Bdsl96ZN7QGKxsQewXnggvlPaURxeX+hrmrhkoejWXqQIDXkMH7RV1unzvZS/CXh6i/joid/0bspvXXbW2Ak8chOz+wgTag6Trz0k9OnIKwcQQ/vY0/3Nz/EzI4AJNCSr18eGDvcLLp3zoPo13d75K1B4kxOkemEE30n1MESowxiM4psuOyjUitlj/e3UMygzHN/iDKip3yj5MfgAJltJhWX+3FhkRtKKpOLNTt1HPZqrAMrpfpBi9LnIlk57qcYqUzXUZ2VV36MbxUQy5AgMBAAECggEAKPv9/pB+Rq3/hDHFIsRnvvaNLeRphVV2r4EssEudh1w8OXynsZ+tCQmTqN7UiibNSwZDUXt0TrnSGXiM7ZirIiJAtpt9EKszi/9kINAHbLExKq/fDiv/9mDcMT3D2ue6yl/Ln+c6HKHrxO9qsHnV835WZmAFWxlM/E6NGhKE6HTfH4dqDqzqKatsA7SjlDmG4KaGbebQgu+SOhI3DVpp2Ji8QcV3VwCTIqaUSp13nYNzi5TvyeRro3lVb2jBwEmrvWyTll/rTLfPkVA1/DA3+kmnUytHyjiL+WIVxJIjdNzAm6y8+OBa/A7JFFeYwa/97NqSCEoB29hsRmrDKjzN5QKBgQD19k2xON0CUYvVj+FZRLyt+m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kAeSHMFgwYBIx8+XC+vEuebigK3+iCanR9UvkFGduicICuBqmAAWcAcy+JiENrLD066MMqmPNCIdWZ9wNvmSTznO/D71qKscDc9xQMpfZ2GPMpXv4FYJhZq1lGAQijA0xJGEBlhBztOE0QlrRXI1DYhRqlyEmKYlcBcvBU6aA0SQ/wIDAQAB";
        //签名方式
        String sign_type="RSA2";
        //编码格式
        String CHARSET="utf-8";
        //正式环境支付宝网关，如果是沙箱环境需更改成https://openapi.alipaydev.com/gateway.do
        String url="https://openapi.alipay.com/gateway.do";
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient(url, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY,sign_type);
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称：alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody("我是测试数据");
        model.setSubject("App支付测试Java");
        //请保证OutTradeNo值每次保证唯一
        model.setOutTradeNo("2017090080001939235");
        model.setTimeoutExpress("30m");
        model.setTotalAmount("0.01");
        model.setProductCode("QUICK_MSECURITY_PAY");
        request.setBizModel(model);
        request.setNotifyUrl("商户外网可以访问的异步地址");
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            System.out.println(response.getBody());//就是orderString 可以直接给客户端请求，无需再做处理。
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }
}
